package com.shuiyihan.design.behavior.observer;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 闹钟 通知
 *
 * @author zhiyuan
 * @since 2021/8/6
 **/
public class Clock {
    //一对多,需要接到通知的对象
    private List<AbstractInfo> infos = new ArrayList<>();

    public void updatetime() {
        System.out.println("12点,通知大家吃饭,吃了睡觉!");
        //通知
        update();
    }


    //添加要得到通知的对象
    public void addInfo(AbstractInfo info) {
         infos.add(info);
    }

    //通知
    private void update() {
        for (AbstractInfo info : infos) {
            info.message();
        }
    }


    public static void main(String[] args) {
        Clock clock = new Clock();
        AbstractInfo eat = new EateInfo();
        AbstractInfo sleep = new SleepInfo();

        clock.addInfo(eat);
        clock.addInfo(sleep);

        //12点的时间到了后,通知监听对象发生变化
        clock.update();
    }
}
